package com.qdxwx.manage.controller.notice;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.notice.NoticeService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.notice.Notice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 公告 信息操作处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController
{
    private String prefix = "notice";

    @Autowired
    private NoticeService noticeService;

    @Permissions("notice:list")
    @GetMapping("/list.html")
    public String notice() {
        return prefix + "/notice-list";
    }

    /**
     * 查询公告列表
     */
    @Permissions("notice:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request){
        int noticeType = TKConvert.toInteger(request.getParameter("noticeType"));
        Notice notice = new Notice();
        notice.setNoticeTitle(request.getParameter("noticeTitle"));
        notice.setCreateBy(request.getParameter("createBy"));
        notice.setNoticeType(noticeType);
        List<Notice> list = noticeService.getList(notice, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(noticeService.count(notice));
        return rspData;
    }

    /**
     * 新增公告
     */
    @GetMapping("/add.html")
    public String add()
    {
        return prefix + "/notice-add";
    }

    /**
     * 新增保存公告
     */
    @Permissions("notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request){
        String noticeTitle = request.getParameter("noticeTitle");
        if(TKString.isEmpty(noticeTitle)) return error("标题不能为空");
        int noticeType = TKConvert.toInteger(request.getParameter("noticeType"));
        if(noticeType < 1) return error("请选择类型");
        Notice notice = new Notice();
        notice.setNoticeTitle(noticeTitle);
        notice.setNoticeType(noticeType);
        notice.setNoticeContent(request.getParameter("noticeContent"));
        notice.setStatus(TKConvert.toInteger(request.getParameter("status")));
        notice.setCreateBy(SessionUtils.getLoginName());
        return toAjax(noticeService.insert(notice));
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") long noticeId, ModelMap mmap){
        if(noticeId < 1) return error404;
        Notice notice = noticeService.get(noticeId);
        if (notice == null) return error500;
        mmap.put("notice", notice);
        return prefix + "/notice-edit";
    }

    /**
     * 修改保存公告
     */
    @Permissions("notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request){
        String noticeTitle = request.getParameter("noticeTitle");
        if(TKString.isEmpty(noticeTitle)) return error("标题不能为空");
        int noticeType = TKConvert.toInteger(request.getParameter("noticeType"));
        if(noticeType < 1) return error("请选择类型");
        long noticeId = TKConvert.toInteger(request.getParameter("noticeId"));
        if(noticeId < 1) return error("缺少参数id");
        Notice notice = noticeService.get(noticeId);
        if(notice == null) return error("找不到对应的通知公告");
        notice.setNoticeTitle(noticeTitle);
        notice.setNoticeType(noticeType);
        notice.setNoticeContent(request.getParameter("noticeContent"));
        notice.setStatus(TKConvert.toInteger(request.getParameter("status")));
        notice.setUpdateBy(SessionUtils.getLoginName());
        return toAjax(noticeService.update(notice));
    }

    /**
     * 删除公告
     */
    @Permissions("notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(noticeService.deleteByIds(ids));
    }
}
